home *** CD-ROM | disk | FTP | other *** search
/ Amiga Magazin: Amiga-CD 1997 November & December / Amiga-CD 1997 #11-12.iso / pd-disketten / ungepackt / 9_95 / apd-9-95-2 / png_dt / viewdt / viewdt.c < prev   
C/C++ Source or Header  |  1996-01-25  |  12KB  |  396 lines

  1. /*
  2.    ViewDT
  3.  
  4.    Simple DataType-based Picture Viewer
  5.  
  6.    Syntax:
  7.       ViewDT FILES/A
  8.  
  9.    Examples:
  10.       ViewDT pictures:space/#?.pic
  11.       ViewDT portrait.png
  12.  
  13.    Status:
  14.       Public Domain
  15.  
  16.    If you require more information please send E-mail to <info@cloanto.it>
  17. */
  18.  
  19. #include <exec/types.h>
  20. #include <exec/memory.h>
  21. #include <graphics/gfx.h>
  22. #include <graphics/displayinfo.h>
  23. #include <intuition/intuitionbase.h>
  24. #include <intuition/gadgetclass.h>
  25. #include <datatypes/datatypes.h>
  26. #include <datatypes/datatypesclass.h>
  27. #include <datatypes/pictureclass.h>
  28. #include <proto/exec.h>
  29. #include <proto/dos.h>
  30. #include <proto/graphics.h>
  31. #include <proto/intuition.h>
  32. #include <proto/datatypes.h>
  33. #include <string.h>
  34. #include <stdio.h>
  35.  
  36. struct Picture
  37. {
  38.    struct BitMapHeader bmhd;  /* format and infos */
  39.    struct BitMap *bmap;       /* bitmap */
  40.    ULONG *palette;            /* color table in LoadRGB32() format */
  41.    LONG palette_size;         /* mem usage */
  42.    LONG palette_entries;      /* number of colors */
  43.    ULONG display_ID;          /* video mode */
  44.    UBYTE *author;             /* author info */
  45.    UBYTE *copyright;          /* copyright info */
  46.    UBYTE *annotation;         /* other info */
  47.    LONG author_size;          /* mem usage */
  48.    LONG copyright_size;       /* mem usage */
  49.    LONG annotation_size;      /* mem usage */
  50. };
  51.  
  52. void FreePicture(struct Picture *pic);
  53. LONG GetDataTypesPicture(UBYTE *file_name, struct Picture *pic, ULONG);
  54. BOOL IsDataTypes(UBYTE *file_name, UBYTE *name_buff, LONG nbuff_size);
  55. BOOL ViewPicture(struct Picture *pic);
  56.  
  57. struct IntuitionBase *IntuitionBase;
  58. struct GfxBase *GfxBase;
  59. struct Library *DataTypesBase;
  60.  
  61.  
  62.  
  63. /*
  64.    IsDataTypes
  65.  
  66.    Parameters
  67.       file_name: name of the file to inspect
  68.       name_buff: (optional) buffer to store the file format name
  69.       nbuff_size: size of name_buff
  70.  
  71.    Return value
  72.       TRUE if DataTypes recognized the file as a valid picture file
  73.       FALSE otherwise
  74. */
  75. BOOL IsDataTypes(UBYTE *file_name, UBYTE *name_buff, LONG nbuff_size)
  76. {
  77.    struct DataType *dtn;
  78.    struct DataTypeHeader *dth;
  79.    BPTR lock;
  80.    BOOL it_is;
  81.  
  82.    it_is = FALSE;
  83.    if (lock = Lock(file_name, ACCESS_READ))
  84.    {
  85.       /* inspect file */
  86.       if (dtn = ObtainDataTypeA(DTST_FILE, (APTR)lock, NULL))
  87.       {
  88.          dth = dtn->dtn_Header;
  89.          if (dth->dth_GroupID == GID_PICTURE)   /* is it a picture? */
  90.          {
  91.             it_is = TRUE;
  92.             if (name_buff)
  93.             {
  94.                strncpy(name_buff, dth->dth_Name, nbuff_size);
  95.                *(name_buff + nbuff_size - 1) = 0;  /* safe strncpy() termination */
  96.             }
  97.          }
  98.          ReleaseDataType(dtn);
  99.       }
  100.       UnLock(lock);
  101.    }
  102.    return(it_is);
  103. }
  104.  
  105. /*
  106.    GetDataTypesPicture
  107.  
  108.    Parameters
  109.       file_name: name of the file to load
  110.       pic: work structure
  111.       bmap_flags: AllocBitMap() flags (e.g. BMF_DISPLAYABLE, BMF_INTERLEAVED etc.)
  112.  
  113.    Return value
  114.       0 if successful (picture info and data in "pic" structure) or
  115.       error code as from dos.library IoErr() function
  116. */
  117. LONG GetDataTypesPicture(UBYTE *file_name, struct Picture *pic, ULONG bmap_flags)
  118. {
  119.    Object *obj;
  120.    struct BitMapHeader *bmh;
  121.    struct BitMap *bmap;
  122.    struct gpLayout layout;
  123.    ULONG *creg, *ctab;
  124.    UBYTE *str;
  125.    LONG ncol, crsize, err;
  126.  
  127.    memset(pic, 0, sizeof(struct Picture));   /* clear pic structure */
  128.    err = 0;
  129.  
  130.    if (obj = NewDTObject(file_name,
  131.                          DTA_SourceType, DTST_FILE,
  132.                          DTA_GroupID, GID_PICTURE,
  133.                          PDTA_Remap, FALSE,
  134.                          TAG_DONE))    /* get the picture object */
  135.    {
  136.       if (GetDTAttrs(obj,
  137.                      PDTA_ModeID, &pic->display_ID,
  138.                      PDTA_BitMapHeader, &bmh,
  139.                      TAG_DONE) == 2)   /* get the bitmap_header and mode_id */
  140.       {
  141.          pic->bmhd = *bmh;
  142.  
  143.          /*
  144.             query the object about its author, copyright and annotation
  145.          */
  146.          if (GetDTAttrs(obj, DTA_ObjAuthor, &str, TAG_DONE) == 1)
  147.          {
  148.             if (str)
  149.             {
  150.                pic->author_size = strlen(str) + 1;
  151.                if (pic->author = AllocMem(pic->author_size, 0))
  152.                   strcpy(pic->author, str);
  153.             }
  154.          }
  155.          if (GetDTAttrs(obj, DTA_ObjCopyright, &str, TAG_DONE) == 1)
  156.          {
  157.             if (str)
  158.             {
  159.                pic->copyright_size = strlen(str) + 1;
  160.                if (pic->copyright = AllocMem(pic->copyright_size, 0))
  161.                   strcpy(pic->copyright, str);
  162.             }
  163.          }
  164.          if (GetDTAttrs(obj, DTA_ObjAnnotation, &str, TAG_DONE) == 1)
  165.          {
  166.             if (str)
  167.             {
  168.                pic->annotation_size = strlen(str) + 1;
  169.                if (pic->annotation = AllocMem(pic->annotation_size, 0))
  170.                   strcpy(pic->annotation, str);
  171.             }
  172.          }
  173.  
  174.          layout.MethodID = DTM_PROCLAYOUT;   /* render the object */
  175.          layout.gpl_GInfo = NULL;
  176.          layout.gpl_Initial = TRUE;
  177.  
  178.          if (DoDTMethodA(obj, NULL, NULL, (Msg)&layout))
  179.          {
  180.             if (GetDTAttrs(obj,
  181.                            PDTA_BitMap, &bmap,
  182.                            PDTA_CRegs, &creg,
  183.                            PDTA_NumColors, &ncol,
  184.                            TAG_DONE) == 3)   /* get the bitmap and its colors */
  185.             {
  186.                crsize = (ncol * 3) * 4;
  187.                pic->palette_entries = ncol;
  188.                pic->palette_size = crsize + (2 * 4);  /* LoadRGB32() table requirements */
  189.  
  190.                if (pic->palette = AllocMem(pic->palette_size, 0))
  191.                {
  192.                   ctab = pic->palette;
  193.                   *ctab++ = (ncol << 16) | 0;   // number of colors and first color to load
  194.                   memcpy(ctab, creg, crsize);
  195.                   *(ctab + (crsize / 4)) = 0;   // terminator
  196.                }
  197.                else err = ERROR_NO_FREE_STORE;
  198.  
  199.                if (pic->bmap = AllocBitMap(pic->bmhd.bmh_Width, pic->bmhd.bmh_Height, pic->bmhd.bmh_Depth, bmap_flags, NULL))
  200.                {
  201.                   BltBitMap(bmap, 0,0, pic->bmap, 0,0, pic->bmhd.bmh_Width, pic->bmhd.bmh_Height, 0xC0, 0xFF, NULL);
  202.                   WaitBlit();
  203.                }
  204.                else err = ERROR_NO_FREE_STORE;
  205.             }
  206.             else err = IoErr();
  207.          }
  208.          else err = IoErr();
  209.       }
  210.       else err = ERROR_REQUIRED_ARG_MISSING;
  211.  
  212.       DisposeDTObject(obj);   /* free the object */
  213.    }
  214.    else err = IoErr();
  215.  
  216.    if (err)
  217.       FreePicture(pic);
  218.  
  219.    return(err);
  220. }
  221.  
  222. /*
  223.    FreePicture
  224.  
  225.    Parameters
  226.       pic: Picture structure with resources to free
  227.  
  228.    Return value
  229.       none
  230. */
  231. void FreePicture(struct Picture *pic)
  232. {
  233.    if (pic->bmap)
  234.    {
  235.       WaitBlit();
  236.       FreeBitMap(pic->bmap);
  237.    }
  238.    if (pic->palette)
  239.       FreeMem(pic->palette, pic->palette_size);
  240.  
  241.    if (pic->author)
  242.       FreeMem(pic->author, pic->author_size);
  243.  
  244.    if (pic->copyright)
  245.       FreeMem(pic->copyright, pic->copyright_size);
  246.  
  247.    if (pic->annotation)
  248.       FreeMem(pic->annotation, pic->annotation_size);
  249.  
  250.    memset(pic, 0, sizeof(struct Picture));   /* clear it all */
  251. }
  252.  
  253. /*
  254.    ViewPicture
  255.  
  256.    Parameters
  257.       pic: picture infos and data
  258.  
  259.    Return value
  260.       TRUE if the user cancelled the view sequence (<Esc> key)
  261.       FALSE otherwise
  262. */
  263. BOOL ViewPicture(struct Picture *pic)
  264. {
  265.    struct Screen *scr;
  266.    struct Window *win;
  267.    struct IntuiMessage *imsg;
  268.    BOOL done, quit;
  269.  
  270.    done = quit = FALSE;
  271.    if (scr = OpenScreenTags(NULL,
  272.                SA_Width, pic->bmhd.bmh_Width,
  273.                SA_Height, pic->bmhd.bmh_Height,
  274.                SA_Depth, pic->bmhd.bmh_Depth,
  275.                SA_Quiet, TRUE,
  276.                SA_ShowTitle, FALSE,    /* no title bar */
  277.                SA_Behind, TRUE,
  278.                SA_Type, CUSTOMSCREEN,
  279.                SA_DisplayID, pic->display_ID,
  280.                SA_Overscan, OSCAN_TEXT,
  281.                SA_AutoScroll, TRUE,
  282.                SA_Colors32, pic->palette,
  283.                SA_BackFill, LAYERS_NOBACKFILL,  /* no screen-clearing when the window is closed (is faster) */
  284.                TAG_END))
  285.    {
  286.       if (win = OpenWindowTags(NULL,
  287.                WA_Width, scr->Width,
  288.                WA_Height, scr->Height,
  289.                WA_IDCMP, MOUSEBUTTONS | VANILLAKEY,
  290.                WA_CustomScreen, scr,
  291.                WA_Backdrop, TRUE,
  292.                WA_Borderless, TRUE,
  293.                WA_Activate, TRUE,
  294.                WA_RMBTrap, TRUE,
  295.                WA_SimpleRefresh, TRUE,
  296.                WA_BackFill, LAYERS_NOBACKFILL,  /* no screen-clearing when the window is opened (is faster) */
  297.                TAG_END))
  298.       {
  299.          BltBitMap(pic->bmap, 0,0, scr->RastPort.BitMap, 0,0, pic->bmhd.bmh_Width, pic->bmhd.bmh_Height, 0xC0, 0xFF, NULL);
  300.          WaitBlit();
  301.          ScreenToFront(scr);  /* show the screen only when the picture has been copied to it */
  302.  
  303.          while (!done)
  304.          {
  305.             Wait(1 << win->UserPort->mp_SigBit);
  306.  
  307.             while (imsg = (struct IntuiMessage *)GetMsg(win->UserPort))
  308.             {
  309.                switch (imsg->Class)
  310.                {
  311.                   case VANILLAKEY:
  312.                      switch (imsg->Code)
  313.                      {
  314.                         case 27:  done = quit = TRUE;  break;  /* <Esc> = cancel */
  315.                         case 13:
  316.                         case 32:  done = TRUE;  break;   /* <Enter> / <Space> = continue */
  317.                      }
  318.                      break;
  319.                   case MOUSEBUTTONS:
  320.                      switch (imsg->Code)
  321.                      {
  322.                         case SELECTUP:    /* MButton = continue */
  323.                         case MIDDLEUP:
  324.                         case MENUUP:   done = TRUE;  break;
  325.                      }
  326.                      break;
  327.                }
  328.                ReplyMsg((struct Message *)imsg);
  329.             }
  330.          }
  331.          CloseWindow(win);
  332.       }
  333.       CloseScreen(scr);
  334.    }
  335.    return(quit);
  336. }
  337.  
  338. void main(int argc, char *argv[])
  339. {
  340.    #define AP_BUFFSIZE  240
  341.    #define PT_SIZE      80
  342.    struct Picture pic;
  343.    struct AnchorPath *ap;
  344.    UBYTE pic_type[PT_SIZE];
  345.    LONG err;
  346.    BOOL quit;
  347.  
  348.    if (argc > 1)
  349.    {
  350.       if (IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 39))
  351.       {
  352.          if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39))
  353.          {
  354.             if (DataTypesBase = OpenLibrary("datatypes.library", 39))
  355.             {
  356.                if (ap = AllocMem(sizeof(struct AnchorPath) + AP_BUFFSIZE, MEMF_CLEAR))
  357.                {
  358.                   ap->ap_Strlen = AP_BUFFSIZE;
  359.                   for (err = MatchFirst(argv[1], ap); err == 0; err = MatchNext(ap))
  360.                   {
  361.                      if (IsDataTypes(ap->ap_Buf, pic_type, PT_SIZE))
  362.                      {
  363.                         if (GetDataTypesPicture(ap->ap_Buf, &pic, 0) == 0)
  364.                         {
  365.                            printf("%s (%s, %dx%d, %d colors)\n",
  366.                                     ap->ap_Info.fib_FileName,
  367.                                     pic_type,
  368.                                     pic.bmhd.bmh_Width,
  369.                                     pic.bmhd.bmh_Height,
  370.                                     pic.palette_entries);
  371.                            if (pic.author)
  372.                               printf("      author: %s\n", pic.author);
  373.                            if (pic.copyright)
  374.                               printf("   copyright: %s\n", pic.copyright);
  375.                            if (pic.annotation)
  376.                               printf("  annotation: %s\n", pic.annotation);
  377.  
  378.                            quit = ViewPicture(&pic);
  379.                            FreePicture(&pic);
  380.                            if (quit)
  381.                               break;
  382.                         }
  383.                      }
  384.                   }
  385.                   MatchEnd(ap);
  386.                   FreeMem(ap, sizeof(struct AnchorPath) + AP_BUFFSIZE);
  387.                }
  388.                CloseLibrary(DataTypesBase);
  389.             }
  390.             CloseLibrary((struct Library *)GfxBase);
  391.          }
  392.          CloseLibrary((struct Library *)IntuitionBase);
  393.       }
  394.    }
  395. }
  396.